%%%%%%%%%%%%%%%%%%%% Table 4 and Figure 4 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc
clear
close all

load RTDB_RATIOS
%%%%%%%%%%%%%%%%%%%% Apparent labour prodactivity %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A=App_lab_prod(167:end,:);

for ik=1:24
    for j=2:20
        YY(j-1,ik)=A(j+1,(j-1)*2+j+ik-1);
    end
end
StdQ=nanstd(YY-YY(:,end)*ones(1,24));
MeanQ=nanmean(YY-YY(:,end)*ones(1,24));

Metot=nanmean(A(:,end));
Stdtot=nanstd(A(:,end));
TAB_agr(:,1)=[Metot; MeanQ([4 6 8 12 16])';Stdtot; StdQ([4 6 8 12 16])'];

lo=find(isfinite(App_lab_prod(:,3)),1,'last');
la=find(isfinite(App_lab_prod(:,end)),1,'last');
T=size(App_lab_prod,1);
last_obs=nan(T,1);
for i=lo:la
    sl=find(isfinite(App_lab_prod(i,:)));
    last_obs(i,:)=App_lab_prod(i,sl(1));
end
dates=datevec(dates);
ss=find(dates(:,1)==1994 & dates(:,2)==3);
ee=find(dates(:,1)==2009 & dates(:,2)==3);
dates=datenum(dates);

figure(1)
plot(dates(ss:ee,:),App_lab_prod(ss:ee,:),dates(ss:ee,:),last_obs(ss:ee,:),'x-r','LineWidth',2,'MarkerSize',15),...
    datetick('x',12),xlabel('time'),axis([dates(ss) dates(ee) 0 8]),title('EA Apparent labour prodactivity gains')
%%%%%%%%%%%%%%%%%%%% Propensity to consume %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A=Prop_cons(167:end,:);

for ik=1:24
    for j=2:20
        YY(j-1,ik)=A(j+1,(j-1)*2+j+ik-1);
    end
end
StdQ=nanstd(YY-YY(:,end)*ones(1,24));
MeanQ=nanmean(YY-YY(:,end)*ones(1,24));

Metot=nanmean(A(:,end));
Stdtot=nanstd(A(:,end));
TAB_agr(:,2)=[Metot; MeanQ([4 6 8 12 16])';Stdtot; StdQ([4 6 8 12 16])'];

lo=find(isfinite(Prop_cons(:,1)),1,'last');
la=find(isfinite(Prop_cons(:,end)),1,'last');
T=size(Prop_cons,1);
last_obs=nan(T,1);
for i=lo:la
    sl=find(isfinite(Prop_cons(i,:)));
    last_obs(i,:)=Prop_cons(i,sl(1));
end

dates=datevec(dates);
ss=find(dates(:,1)==1991 & dates(:,2)==3);
ee=find(dates(:,1)==2009 & dates(:,2)==3);
dates=datenum(dates);

figure(2)
plot(dates(ss:ee,:),Prop_cons(ss:ee,:),dates(ss:ee,:),last_obs(ss:ee,:),'x-r','LineWidth',2,'MarkerSize',15),...
    datetick('x',12),xlabel('time'),axis([dates(ss) dates(ee) 55 61]),title('EA Propensity to consume')
%%%%%%%%%%%%%%%%%%%% Propensity to invest %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A=Prop_inv(167:end,:);

for ik=1:24
    for j=2:20
        YY(j-1,ik)=A(j+1,(j-1)*2+j+ik-1);
    end
end
StdQ=nanstd(YY-YY(:,end)*ones(1,24));
MeanQ=nanmean(YY-YY(:,end)*ones(1,24));

Metot=nanmean(A(:,end));
Stdtot=nanstd(A(:,end));
TAB_agr(:,3)=[Metot; MeanQ([4 6 8 12 16])';Stdtot; StdQ([4 6 8 12 16])'];

lo=find(isfinite(Prop_inv(:,1)),1,'last');
la=find(isfinite(Prop_inv(:,end)),1,'last');
T=size(Prop_inv,1);
last_obs=nan(T,1);
for i=lo:la
    sl=find(isfinite(Prop_inv(i,:)));
    last_obs(i,:)=Prop_inv(i,sl(1));
end

dates=datevec(dates);
ss=find(dates(:,1)==1991 & dates(:,2)==3);
ee=find(dates(:,1)==2009 & dates(:,2)==3);
dates=datenum(dates);

figure(3)
plot(dates(ss:ee,:),Prop_inv(ss:ee,:),dates(ss:ee,:),last_obs(ss:ee,:),'x-r','LineWidth',2,'MarkerSize',15),...
    datetick('x',12),xlabel('time'),axis([dates(ss) dates(ee) 19 23]),title('EA Propensity to invest')
%%%%%%%%%%%%%%%%%%%% Internal Terms of Trade %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A=Int_trade(167:end,:);

for ik=1:24
    for j=2:20
        YY(j-1,ik)=A(j+1,(j-1)*2+j+ik-1);
    end
end
StdQ=nanstd(YY-YY(:,end)*ones(1,24));
MeanQ=nanmean(YY-YY(:,end)*ones(1,24));

Metot=nanmean(A(:,end));
Stdtot=nanstd(A(:,end));
TAB_agr(:,4)=[Metot; MeanQ([4 6 8 12 16])';Stdtot; StdQ([4 6 8 12 16])'];

lo=find(isfinite(Int_trade(:,1)),1,'last');
la=find(isfinite(Int_trade(:,end)),1,'last');
T=size(Int_trade,1);
last_obs=nan(T,1);
for i=lo:la
    sl=find(isfinite(Int_trade(i,:)));
    last_obs(i,:)=Int_trade(i,sl(1));
end

dates=datevec(dates);
ss=find(dates(:,1)==1991 & dates(:,2)==3);
ee=find(dates(:,1)==2009 & dates(:,2)==3);
dates=datenum(dates);

figure(4)
plot(dates(ss:ee,:),Int_trade(ss:ee,:),dates(ss:ee,:),last_obs(ss:ee,:),'x-r','LineWidth',2,'MarkerSize',15),...
    datetick('x',12),xlabel('time'),axis([dates(ss) dates(ee) 97 103]),title('EA Internal Terms of Trade')

%%%%%%%%%%%%%%%%%%%% Trade Openess %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A=Trade_Open(167:end,:);

for ik=1:24
    for j=2:20
        YY(j-1,ik)=A(j+1,(j-1)*2+j+ik-1);
    end
end
StdQ=nanstd(YY-YY(:,end)*ones(1,24));
MeanQ=nanmean(YY-YY(:,end)*ones(1,24));

Metot=nanmean(A(:,end));
Stdtot=nanstd(A(:,end));
TAB_agr(:,5)=[Metot; MeanQ([4 6 8 12 16])';Stdtot; StdQ([4 6 8 12 16])'];

lo=find(isfinite(Trade_Open(:,1)),1,'last');
la=find(isfinite(Trade_Open(:,end)),1,'last');
T=size(Trade_Open,1);
last_obs=nan(T,1);
for i=lo:la
    sl=find(isfinite(Trade_Open(i,:)));
    last_obs(i,:)=Trade_Open(i,sl(1));
end

dates=datevec(dates);
ss=find(dates(:,1)==1991 & dates(:,2)==3);
ee=find(dates(:,1)==2009 & dates(:,2)==3);
dates=datenum(dates);

figure(5)
plot(dates(ss:ee,:),Trade_Open(ss:ee,:),dates(ss:ee,:),last_obs(ss:ee,:),'x-r','LineWidth',2,'MarkerSize',15),...
    datetick('x',12),xlabel('time'),axis([dates(ss) dates(ee) 20 50]),title('EA Trade Openess')


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
TAB_agr=round(TAB_agr*100)/100;

disp(' ')
disp('--------------------------------------------------------------------------------------------------------------------')

disp('                      Apparent                                Internal                ')
disp('                      Labor        Prop.    Prop.  Terms      Trade')
disp('                      Product.     to       to     of         Openess')
disp('                      Gains        Cons.    Inv.   Trade      Differences')

disp(['Mean','                      ', num2str(TAB_agr(1,:),2)])
disp('Mean of revisions')
disp(['Y_{t|t+4}-Y_{t|t+24}','     ', num2str(TAB_agr(2,:),2)])
disp(['Y_{t|t+6}-Y_{t|t+24}','     ', num2str(TAB_agr(3,:),2)])
disp(['Y_{t|t+8}-Y_{t|t+24}','     ', num2str(TAB_agr(4,:),2)])
disp(['Y_{t|t+12}-Y_{t|t+24}','    ', num2str(TAB_agr(5,:),2)])
disp(['Y_{t|t+16}-Y_{t|t+24}','    ', num2str(TAB_agr(6,:),2)])
disp(['Standard deviation','        ', num2str(TAB_agr(7,:),2)])
disp('Standard deviation of revisions')
disp(['Y_{t|t+4}-Y_{t|t+24}','      ', num2str(TAB_agr(8,:),2)])
disp(['Y_{t|t+6}-Y_{t|t+24}','       ', num2str(TAB_agr(9,:),2)])
disp(['Y_{t|t+8}-Y_{t|t+24}','      ', num2str(TAB_agr(10,:),2)])
disp(['Y_{t|t+12}-Y_{t|t+24}','      ', num2str(TAB_agr(11,:),2)])
disp(['Y_{t|t+16}-Y_{t|t+24}','     ', num2str(TAB_agr(12,:),2)])
